const mongoose = require("mongoose");
const FileData = require("../models/FileDataModel");

exports.addFileData = async (req, res) => {
  try {
    const newFileData = await FileData.create(req.body);
    res.status(201).json({
      status: "success",
      data: newFileData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getFileData = async (req, res) => {
  try {
    const FileDataData = await FileData.find();
    res.status(201).json({
      status: "success",
      data: FileDataData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getFileDataById = async (req, res) => {
  let id = req.params.id;
  try {
    const FileDataData = await FileData.findOne({ _id: id });
    res.status(201).json({
      status: "success",
      data: FileDataData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.updateFileData = async (req, res) => {
  console.log("更新入户资料---", req.body._id);
  try {
    const FileDataData = await FileData.updateOne(
      { _id: req.body._id },
      req.body
    );
    res.status(201).json({
      status: "success",
      data: FileDataData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};
